home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / C Internet Config / IC Application Source ƒ / C Source ƒ / IC File Map What.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-02  |  20.7 KB  |  848 lines  |  [TEXT/SPM ]

  1. /*
  2.     IC File Map What.c
  3.     
  4. */
  5.  
  6. #include <Icons.h>
  7. #include <ToolUtils.h>
  8. #include <LowMem.h>
  9.  
  10. #include "IC Window Globals.h"
  11. #include "IC StrH.h"
  12. #include "IC Types.h"
  13. #include "IC Keys.h"
  14. #include "IC API.h"
  15. #include "IC Subs.h"
  16. #include "IC Dialogs.h"
  17. #include "IC Misc Subs.h"
  18. #include "IC Document.h"
  19. #include "IC Globals.h"
  20. #include "IC IconSuites.h"
  21. #include "IC StandardFile.h"
  22. #include "IC Movable Modal.h"
  23. #include "IC Windows.h"
  24.  
  25. #include "IC File Map What.h"
  26.  
  27. #include "SpinLib.h"
  28.  
  29. void DimButtons(WindowType wt){
  30.     ListHandle lh=(ListHandle)WindowInfo[wt].items[ditList]->spare_data;
  31.     DialogPtr dlg=WindowInfo[wt].window;
  32.     Boolean have_selection=(SelectedLine(lh)!=-1);
  33.     Boolean unlok=!IsLocked(wt,ditList);
  34.     
  35.     SetDCtlEnable(dlg,ditAdd,unlok);
  36.     SetDCtlEnable(dlg,ditDelete,have_selection&&unlok);
  37.     SetDCtlEnable(dlg,ditChange,have_selection&&unlok);
  38.     SetDCtlEnable(dlg,ditDefaults,unlok);
  39.     
  40.     OutlineDefault1(dlg,ditMainUserItem);
  41. }
  42.  
  43. void LDClose(void){
  44.     
  45. }
  46.  
  47. void LDDraw(Handle entries,Boolean select,Rect* r,Cell the_cell,short offset,short datalen,ListHandle lh){
  48.     ICMapEntry entry;
  49.     OSErr err;
  50.     long junkpos;
  51.     Handle suite;
  52.     short cur_x,top,transfer=ttNone;
  53.     Rect tmprect;
  54.     FontInfo sys_font_info,appl_font_info;
  55.     RgnHandle rgn=(RgnHandle)0,rgn2;
  56.     Str255 astr,bstr;
  57.     
  58.     if (datalen==0){
  59.         err=ICMapErr(ICGetIndMapEntry(GetInstance(),entries,the_cell.v+1,&junkpos,&entry));
  60.         if (err==noErr){
  61.             EraseRect(r);
  62.             cur_x=0;
  63.             rgn=NewRgn();
  64.             if (System7){
  65.                 tmprect.top=r->top;
  66.                 tmprect.left=r->left;
  67.                 tmprect.bottom=tmprect.top+32;
  68.                 tmprect.right=tmprect.left+32;
  69.                 
  70.                 if (kCellHeight!=32)
  71.                     OffsetRect(&tmprect,(kCellHeight-32)/2,(kCellHeight-32)/2);
  72.                 
  73.                 GetDTDBIconSuiteCached(entry.file_type,entry.file_creator,&suite);
  74.                 if (suite!=(Handle)0){
  75.                     if (select)
  76.                         transfer=ttSelected;
  77.                     err=PlotIconSuite(&tmprect,atNone,transfer,suite);
  78.                     IconSuiteToRgn(rgn,&tmprect,atNone,suite);
  79.                 }
  80.                 cur_x+=kCellHeight;
  81.             }
  82.             cur_x+=2;
  83.             
  84.             TextFont(systemFont);
  85.             GetFontInfo(&sys_font_info);
  86.             TextFont(applFont);
  87.             GetFontInfo(&appl_font_info);
  88.             
  89.             top=(kCellHeight-(sys_font_info.ascent+sys_font_info.descent+appl_font_info.ascent+appl_font_info.descent))/2;
  90.             TextFont(systemFont);
  91.             
  92.             MoveTo(r->left+cur_x,r->top+top+sys_font_info.ascent);
  93.             DrawString(SetPString(astr,3,entry.creator_app_name,GetAString(bstr,128,5),entry.entry_name));
  94.             
  95.             TextFont(applFont);
  96.             MoveTo(r->left+cur_x,r->top+top+sys_font_info.ascent+sys_font_info.descent+appl_font_info.ascent);
  97.             DrawString(entry.extension);
  98.             
  99.             if (select){
  100.                 rgn2=NewRgn();
  101.                 RectRgn(rgn2,r);
  102.                 
  103.                 if (Has_ColorQD){
  104.                     unsigned char hl;
  105.                     
  106.                     hl=LMGetHiliteMode();
  107.                     BitClr((Ptr)&hl,pHiliteBit);
  108.                     LMSetHiliteMode(hl);
  109.                 }
  110.                 
  111.                 XorRgn(rgn,rgn2,rgn2);
  112.                 InvertRgn(rgn2);
  113.                 DisposeRgn(rgn2);
  114.             }
  115.             if (rgn!=(RgnHandle)0)
  116.                 DisposeRgn(rgn);
  117.         }
  118.     }
  119. }
  120.  
  121.  
  122. pascal void MyLDEF(short message,Boolean select,Rect* r,Cell the_cell,short offset,short datalen,ListHandle lh){
  123.     Handle entries;
  124.     
  125.     SetPort((*lh)->port);
  126.     entries=(Handle)WindowInfo[GetWindowType((*lh)->port)].items[ditList]->data;
  127.     
  128.     if (message==lCloseMsg)
  129.         LDClose();
  130.     else if ((message==lDrawMsg)||(message==lHiliteMsg))
  131.         LDDraw(entries,select,r,the_cell,offset,datalen,lh);
  132. }
  133.  
  134. pascal void FileMapUserItemUpdate(DialogPtr dlg,short item){
  135.     Rect list_rect;
  136.     
  137.     GetDItemRect(dlg,item,&list_rect);
  138.     PenNormal();
  139.     InsetRect(&list_rect,-1,-1);
  140.     FrameRect(&list_rect);
  141.     LUpdate(dlg->visRgn,(ListHandle)WindowInfo[GetWindowType(dlg)].items[item]->spare_data);
  142. }
  143.  
  144. OSErr WhatOpenFileMap(WindowType wt,short item){
  145.     DialogPtr dlg=WindowInfo[wt].window;
  146.     Handle entries=(Handle)0;
  147.     long attr;
  148.     OSErr err=noErr;
  149.     long count;
  150.     Rect list_rect,data_rect;
  151.     Point cell_size;
  152.     ListHandle lh;
  153.     
  154.     WindowInfo[wt].items[item]->spare_data=(Ptr)0;
  155.     WindowInfo[wt].items[item]->data=(Ptr)0;
  156.     
  157.     SetDItemHandle(dlg,item,(Handle)gFileMapUserItemUpdate);
  158.     if (ICMapErr(ICGetPrefHandle(GetInstance(),WindowInfo[wt].items[item]->key,&attr,&entries))!=noErr){
  159.         entries=NewHandle(0);
  160.         if (entries==(Handle)0)
  161.             err=memFullErr;
  162.     }
  163.     ProcessAttributes(wt,item,attr);
  164.     
  165.     if (err==noErr){
  166.         if (ICMapErr(ICCountMapEntries(GetInstance(),entries,&count))!=noErr)
  167.             count=0;
  168.         WindowInfo[wt].items[item]->data=(Ptr)entries;
  169.         GetDItemRect(dlg,item,&list_rect);
  170.         list_rect.right-=15;
  171.         SetRect(&data_rect,0,0,1,count);
  172.         SetPt(&cell_size,list_rect.right-list_rect.left,kCellHeight);
  173.         lh=LNew(&list_rect,&data_rect,cell_size,128,dlg,true,false,false,true);
  174.         if (lh==(ListHandle)0)
  175.             err=memFullErr;
  176.     }
  177.     
  178.     if (err==noErr){
  179.         (*lh)->refCon=(long)gFileListLDEF;
  180.         (*lh)->selFlags=lOnlyOne;
  181.         WindowInfo[wt].items[item]->spare_data=(Ptr)lh;
  182.         DimButtons(wt);
  183.         SetUpDefaultOutline(dlg,ditChange,ditMainUserItem);
  184.         WindowInfo[wt].selected_item=ditList;
  185.     }
  186.     
  187.     return err;
  188. }
  189.  
  190. void SetRadio(DialogPtr modal,short value){
  191.     SetDCtlBoolean(modal,ditAsciiRadio,value==ditAsciiRadio);
  192.     SetDCtlBoolean(modal,ditBinaryRadio,value==ditBinaryRadio);
  193.     SetDCtlBoolean(modal,ditMacintoshRadio,value==ditMacintoshRadio);
  194. }
  195.  
  196. pascal Boolean AddChangeFilter(DialogPtr dlg,EventRecord* event,short* item){
  197.     Boolean res;
  198.     char ch;
  199.     
  200.     res=CancelModalFilter(dlg,event,item);
  201.     if (!res){
  202.         if ((event->what==keyDown)&&(event->modifiers&cmdKey)){
  203.             ch=event->message&charCodeMask;
  204.             if ((ch>='1')&&(ch<='3'))
  205.                 SetRadio(dlg,ch-'1'+ditAsciiRadio);
  206.         }
  207.     }
  208.     
  209.     return res;
  210. }
  211.  
  212. OSErr GetMapEntriesResource(Handle* entries){
  213.     OSErr err;
  214.     short orf=CurResFile();
  215.     
  216.     UseResFile(app_resfile);
  217.     *entries=Get1NamedResource('Pref',kICMapping);
  218.     MoveHHi(*entries);
  219.     UseResFile(orf);
  220.     
  221.     err=ResError();
  222.     if ((*entries==(Handle)0)&&(err==noErr))
  223.         err=resNotFound;
  224.     
  225.     return err;
  226. }
  227.  
  228. StringPtr MapOSType(OSType ot,StringPtr str){
  229.     if (ot==(OSType)0)
  230.         str[0]=0;
  231.     else
  232.         OSTypeToString(ot,str);
  233.     
  234.     return str;
  235. }
  236.  
  237. OSType MapStr(StringPtr s){
  238.     OSType typ;
  239.     Str31 tstr;
  240.     
  241.     Concat(s,1,"\p    "); // add 4 spaces
  242.     TPCopy(tstr,s,1,4);
  243.     StringToOSType(tstr,&typ);
  244.     return typ;
  245. }
  246.  
  247.  
  248. void DoChooseExample(DialogPtr modal){
  249.     FInfo info;
  250.     OSErr err;
  251.     FSSpec fs,app_fss;
  252.     Str255 tstr;
  253.     
  254.     err=ICStandardGetFile((OSType)0,&fs,&info);
  255.     DisplayError(acGetExample,err);
  256.     if (err==noErr){
  257.         SetItemText(modal,ditType,MapOSType(info.fdType,tstr));
  258.         SetItemText(modal,ditCreator,MapOSType(info.fdCreator,tstr));
  259.         err=MyGetAPPL(info.fdCreator,&app_fss);
  260.         if (err==noErr)
  261.             SetItemText(modal,ditApplicationName,app_fss.name);
  262.         else
  263.             SetItemText(modal,ditApplicationName,"\p");
  264.     }
  265. }
  266.  
  267. Boolean ChoosePost(ICMapEntry* entry){
  268.     FInfo info;
  269.     OSErr err;
  270.     FSSpec fs;
  271.     
  272.     err=ICStandardGetFile('APPL',&fs,&info);
  273.     DisplayError(acGetExample,err);
  274.     if (err==noErr){
  275.         entry->post_creator=info.fdCreator;
  276.         SetPString(entry->post_app_name,1,fs.name);
  277.     }
  278.     return err==noErr;
  279. }
  280.  
  281. void SetPostInfo(ICMapEntry* entry,DialogPtr modal){
  282.     Str255 tstr,s1,s2;
  283.     
  284.     if (entry->post_creator!=(OSType)0){
  285.         SetPString(tstr,3,GetIndStr(128,24,s1),entry->post_app_name,GetIndStr(128,25,s2));
  286.         SetDCtlTitle(modal,ditPostButton,tstr);
  287.         SetItemText(modal,ditPostCreator,MapOSType(entry->post_creator,tstr));
  288.     } else {
  289.         SetDCtlTitle(modal,ditPostButton,SetPString(tstr,1,GetIndStr(128,23,s1)));
  290.         SetItemText(modal,ditPostCreator,"\p");
  291.     }
  292.     SetDCtlBoolean(modal,ditPost,(entry->flags&(1<<ICmap_post_bit)));
  293. }
  294.  
  295. void SetAllTheStuff(ICMapEntry* entry,DialogPtr modal){
  296.     Boolean bin=BitTst(&entry->flags,ICmap_binary_bit);
  297.     Boolean res=BitTst(&entry->flags,ICmap_resource_fork_bit);
  298.     Str255 str;
  299.     
  300.     SetItemText(modal,ditEntryName,entry->entry_name);
  301.     SetItemText(modal,ditExtension,entry->extension);
  302.     SetItemText(modal,ditMIMEType,entry->MIME_type);
  303.     
  304.     SetDCtlBoolean(modal,ditAsciiRadio,!bin);
  305.     SetDCtlBoolean(modal,ditBinaryRadio,bin&&!res);
  306.     SetDCtlBoolean(modal,ditMacintoshRadio,bin&&res);
  307.     
  308.     SetDCtlBoolean(modal,ditNotForIncoming,BitTst(&entry->flags,ICmap_not_incoming_bit));
  309.     SetDCtlBoolean(modal,ditNotForOutgoing,BitTst(&entry->flags,ICmap_not_outgoing_bit));
  310.     
  311.     SetItemText(modal,ditType,MapOSType(entry->file_type,str));
  312.     SetItemText(modal,ditCreator,MapOSType(entry->file_creator,str));
  313.     
  314.     SetItemText(modal,ditApplicationName,entry->creator_app_name);
  315.     
  316.     SetPostInfo(entry,modal);
  317.     
  318.     SelectDialogItemText(modal,ditEntryName,0,32767);
  319. }
  320.  
  321. void SetAddEditDefaults(ICMapEntry* entry,DialogPtr modal){
  322.     OSErr err;
  323.     Str255 ext;
  324.     Handle entries;
  325.     ICMapEntry new_entry;
  326.     short a;
  327.     
  328.     GetItemText(modal,ditExtension,ext);
  329.     err=GetMapEntriesResource(&entries);
  330.     if (err==noErr){
  331.         err=ICMapErr(ICMapEntriesFilename(GetInstance(),entries,ext,&new_entry));
  332.         ReleaseResource(entries);
  333.     }
  334.     
  335.     if (err==noErr){
  336.         BlockMoveData((Ptr)&new_entry,(Ptr)entry,sizeof(ICMapEntry));
  337.         SetAllTheStuff(entry,modal);
  338.     }
  339.     
  340.     if (err==icPrefNotFoundErr){
  341.         ParamText("\p",ext,"\p","\p");
  342.         StopAlert(166,gOKModalFilter);
  343.     } else
  344.         DisplayError(acSetDefaults,err);
  345. }
  346.  
  347.  
  348. OSErr DoDialog(ICMapEntry* entry){
  349.     DialogPtr modal;
  350.     OSErr err=noErr;
  351.     short item;
  352.     long flags;
  353.     Str255 tmpstr;
  354.     Ptr saved_state;
  355.     Boolean dummy;
  356.     
  357.     modal=GetNewDialog(700,(Ptr)0,(WindowPtr)-1);
  358.     
  359.     if (modal==(DialogPtr)0)
  360.         err=memFullErr;
  361.     
  362.     if (err==noErr){
  363.         SetUpDefaultOutline(modal,ditOK,ditEditorUserItem);
  364.         SetAllTheStuff(entry,modal);
  365.         
  366.         ShowWindow(modal);
  367.         DisableMenuBar(&saved_state,-1);
  368.         InitCursor();
  369.         
  370.         do {
  371.             MovableModalDialog(gAddChangeFilter,&item);
  372.             
  373.             switch (item){
  374.                 case ditAsciiRadio: case ditBinaryRadio: case ditMacintoshRadio:
  375.                     SetRadio(modal,item);
  376.                     break;
  377.                 case ditNotForIncoming: case ditNotForOutgoing:
  378.                     ToggleDCtlBoolean(modal,item);
  379.                     break;
  380.                 case ditChooseExample:
  381.                     DoChooseExample(modal);
  382.                     break;
  383.                 case ditPost:
  384.                     if (!(entry->flags&(1<<ICmap_post_bit))){
  385.                         if (entry->post_creator==(OSType)0){
  386.                             if (entry->file_creator!=(OSType)0){
  387.                                 entry->post_creator=entry->file_creator;
  388.                                 SetPString(entry->post_app_name,1,entry->creator_app_name);
  389.                             } else {
  390.                                 ChoosePost(entry);
  391.                             }
  392.                         }
  393.                         if (entry->post_creator!=(OSType)0) // set the post bit
  394.                             entry->flags |= (1<<ICmap_post_bit);
  395.                     } else {
  396.                         // turn off the bit
  397.                         entry->flags &= (~(1<<ICmap_post_bit));
  398.                     }
  399.                     SetPostInfo(entry,modal);
  400.                     break;
  401.                 case ditPostButton:
  402.                     if (ChoosePost(entry))
  403.                         entry->flags|=(1<<ICmap_post_bit);
  404.                     SetPostInfo(entry,modal);
  405.                     break;
  406.                 case ditAddEditDefaults:
  407.                     SetAddEditDefaults(entry,modal);
  408.                     break;
  409.             }
  410.         } while ((item!=ditOK)&&(item!=ditCancel));
  411.         
  412.         if (item==ditOK){
  413.             long mask;
  414.             
  415.             GetItemText(modal,ditEntryName,entry->entry_name);
  416.             GetItemText(modal,ditExtension,entry->extension);
  417.             GetItemText(modal,ditMIMEType,entry->MIME_type);
  418.             
  419.             mask=0L;
  420.             mask |= ICmap_binary_mask | ICmap_data_fork_mask | ICmap_resource_fork_mask;
  421.             mask |= ICmap_not_incoming_mask | ICmap_not_outgoing_mask;
  422.             
  423.             flags=entry->flags & (~mask); // clear the lower bits from the flag
  424.             flags |= ICmap_data_fork_mask;
  425.             
  426.             if (!GetDCtlBoolean(modal,ditAsciiRadio))
  427.                 flags |= ICmap_binary_mask;
  428.             if (GetDCtlBoolean(modal,ditMacintoshRadio))
  429.                 flags |= ICmap_resource_fork_mask;
  430.             if (GetDCtlBoolean(modal,ditNotForIncoming))
  431.                 flags |= ICmap_not_incoming_mask;
  432.             if (GetDCtlBoolean(modal,ditNotForOutgoing))
  433.                 flags |= ICmap_not_outgoing_mask;
  434.             
  435.             entry->flags=flags;
  436.             
  437.             GetItemText(modal,ditType,tmpstr);
  438.             entry->file_type=MapStr(tmpstr);
  439.             
  440.             GetItemText(modal,ditCreator,tmpstr);
  441.             entry->file_creator=MapStr(tmpstr);
  442.             
  443.             GetItemText(modal,ditApplicationName,entry->creator_app_name);
  444.         } else
  445.             err=userCanceledErr;
  446.     }
  447.     
  448.     if (modal!=(DialogPtr)0){
  449.         ReEnableMenuBar(&saved_state);
  450.         DisposeDialog(modal);
  451.     }
  452.     
  453.     return err;
  454. }
  455.  
  456. OSErr DoAddChange(short selection,ICMapEntry* entry,Handle entries,ListHandle lh){
  457.     OSErr err=noErr;
  458.     long pos;
  459.     
  460.     if (selection==-1){
  461.         entry->version=0;
  462.         entry->file_creator=entry->post_creator=entry->file_type=(OSType)0;
  463.         entry->flags=0L;
  464.         entry->extension[0]=entry->creator_app_name[0]=entry->post_app_name[0]=0;
  465.         entry->MIME_type[0]=entry->entry_name[0]=0;
  466.     } else
  467.         err=ICMapErr(ICGetIndMapEntry(GetInstance(),entries,selection+1,&pos,entry));
  468.     
  469.     if (err==noErr)
  470.         err=DoDialog(entry);
  471.     
  472.     if (err==noErr){
  473.         if (selection==-1){
  474.             err=ICMapErr(ICAddMapEntry(GetInstance(),entries,entry));
  475.             if (err==noErr)
  476.                 LAddRow(1,32767,lh);
  477.         } else 
  478.             err=ICMapErr(ICSetMapEntry(GetInstance(),entries,pos,entry));
  479.     }
  480.     
  481.     return err;
  482. }
  483.  
  484. OSErr SetEntriesHandle(WindowType wt,Handle entries){
  485.     OSErr err;
  486.     Handle tmph;
  487.     long count;
  488.     ListHandle list=(ListHandle)WindowInfo[wt].items[ditList]->spare_data;
  489.     WindowPtr window;
  490.     short row;
  491.     
  492.     err=ICMapErr(ICCountMapEntries(GetInstance(),entries,&count));
  493.     
  494.     if (err==noErr){
  495.         window=GetWindowPtr(wt);
  496.         LDelRow((*list)->dataBounds.bottom+10,0,list);
  497.         tmph=entries;
  498.         entries=(Handle)WindowInfo[wt].items[ditList]->data;
  499.         WindowInfo[wt].items[ditList]->data=(Ptr)tmph;
  500.         row=LAddRow(count,0,list);
  501.     }
  502.     
  503.     DisposeHandle(entries);
  504.     
  505.     return err;
  506. }
  507.  
  508. /*
  509.     The following quicksort implementation is based loosely upon Symantec's qsort ANSI routine.
  510.     In fact, it is their routine except the comparison function is hard coded and the need to use globals
  511.     has been removed.
  512. */
  513. long PartitionSmall(SmallArrayHandle small,register long p,register long r);
  514. long PartitionSmall(SmallArrayHandle small,register long p,register long r){
  515.     register long mid;
  516.     register long j;
  517.     SmallRecordPtr swap;
  518.     
  519.     mid=(p+r)/2;
  520.     p--;
  521.     j=r+1;
  522.     
  523.     while (1){
  524.         while (IUCompString((*small)[--j]->key,(*small)[mid]->key)>0)
  525.             ;
  526.         while (IUCompString((*small)[++p]->key,(*small)[mid]->key)<0)
  527.             ;
  528.         
  529.         if (p<j){
  530.             if (p>mid)
  531.                 mid=p;
  532.             if (j<mid)
  533.                 mid=j;
  534.             
  535.             // switch the pointers in the array
  536.             swap=(*small)[p];
  537.             (*small)[p]=(*small)[j];
  538.             (*small)[j]=swap;
  539.             
  540.             if (p==mid){
  541.                 mid=j++;
  542.             } else if (j==mid){
  543.                 mid=p--;
  544.             }
  545.         } else {
  546.             if (mid==r)
  547.                 return (mid-1);
  548.             else
  549.                 return mid;
  550.         }
  551.     }
  552. }
  553.  
  554. void QuickSortSmall(register SmallArrayHandle small,register long first,register long last){
  555.     long q;
  556.     
  557.     while (first<last){
  558.         q=PartitionSmall(small,first,last);
  559.         QuickSortSmall(small,first,q);
  560.         first=q+1;
  561.     }
  562. }
  563.  
  564. void SmallSort(SmallArrayHandle small,long count){
  565.     
  566.     if (count<1)
  567.         return; // no elements to sort
  568.     
  569.     QuickSortSmall(small,0,count-1);
  570. }
  571.  
  572. /*
  573.     DoSort - Sort the list based on extension, creator, or name.
  574.     
  575.     Unlike the pascal version, I am using an array of pointers to control the sorting.
  576.     Hopefully this would result in a traumatic speed increase when sorting a large list of items,
  577.     assuming I got the translation correct. (speed increase a result of swapping pointers instead of elements).
  578.     
  579.     I also removed the set to the watch cursor and replaced with spinning beachball cursor library.
  580. */
  581. void DoSort(WindowType wt,short item){
  582.     Handle entries=(Handle)WindowInfo[wt].items[ditList]->data,sorted_entries=NewHandle(0);
  583.     SmallArrayHandle small;
  584.     SmallRecord** arr;
  585.     long count,pos,i;
  586.     ICMapEntry entry;
  587.     // CursHandle curs=GetCursor(watchCursor);
  588.     ICInstance inst=GetInstance();
  589.     OSErr err=ICMapErr(ICCountMapEntries(inst,entries,&count));
  590.     StringPtr sp;
  591.     
  592.     // SetCursor(*curs);
  593.     
  594.     SpinStart(kForwardDirection);
  595.     
  596.     PrepMem(); // purge & compact memory
  597.     
  598.     if (err==noErr){
  599.         arr=(SmallRecord**)NewHandle(sizeof(SmallRecord)*(count+2));
  600.         err=MemError();
  601.         if (err==noErr){
  602.             small=(SmallArrayHandle)NewHandle(sizeof(SmallArray)*count);
  603.             err=MemError();
  604.             if (err==noErr){
  605.                 pos=0L;
  606.                 for (i=0;i<count;i++){
  607.                     (*small)[i]=&((*arr)[i]);
  608.                     err=ICMapErr(ICGetMapEntry(inst,entries,pos,&entry));
  609.                     if (err!=noErr){
  610.                         err=-8002;
  611.                         break;
  612.                     }
  613.                     (*small)[i]->position=i+1;
  614.                     
  615.                     if (item==ditSortExtension)
  616.                         sp=entry.extension;
  617.                     else if (item==ditSortApplication)
  618.                         sp=entry.creator_app_name;
  619.                     else if (item==ditSortName)
  620.                         sp=entry.entry_name;
  621.                     else
  622.                         sp=(StringPtr)0;
  623.                     
  624.                     if (sp!=(StringPtr)0)
  625.                         TPCopy((*small)[i]->key,sp,1,31);
  626.                     
  627.                     pos+=entry.total_length;
  628.                 }
  629.                 if (err==noErr){
  630.                     SmallSort(small,count);
  631.                     HLock(entries);
  632.                     
  633.                     for (i=0;i<count;i++){
  634.                         err=ICMapErr(ICGetIndMapEntry(inst,entries,(*small)[i]->position,&pos,&entry));
  635.                         if (err==noErr)
  636.                             err=PtrAndHand((Ptr)(((long)(*entries))+pos),sorted_entries,entry.total_length);
  637.                         if (err!=noErr)
  638.                             break;
  639.                     }
  640.                     if (err)
  641.                         err=-8003;
  642.                     HUnlock(entries);
  643.                 }
  644.                 if (err==noErr){
  645.                     err=SetEntriesHandle(wt,sorted_entries);
  646.                     if (err)
  647.                         err=-8004;
  648.                     sorted_entries=(Handle)0;
  649.                 }
  650.                 DisposeHandle((Handle)small);
  651.                 small=(SmallArrayHandle)0;
  652.             } else err=-8001;
  653.             DisposeHandle((Handle)arr);
  654.             arr=(SmallRecord**)0;
  655.         } else err=-8000;
  656.     }
  657.     
  658.     if (sorted_entries!=(Handle)0)
  659.         DisposeHandle(sorted_entries);
  660.     
  661.     // InitCursor();
  662.     InvalidateAbout();
  663.     SpinStop();
  664.     
  665.     DisplayError(acSort,err);
  666. }
  667.  
  668. void DoSetDefaults(WindowType wt){
  669.     OSErr err;
  670.     Handle entries;
  671.     
  672.     if (CautionAlert(165,gCancelModalFilter)==1){
  673.         err=GetMapEntriesResource(&entries);
  674.         if (err==noErr){
  675.             DetachResource(entries);
  676.             err=SetEntriesHandle(wt,entries);
  677.         }
  678.         DisplayError(acSetDefaults,err);
  679.         if (err==noErr)
  680.             DirtyDocument();
  681.     }
  682. }
  683.  
  684. OSErr WhatClickFileMap(WindowType wt,short item,EventRecord* er){
  685.     ListHandle lh=(ListHandle)WindowInfo[wt].items[ditList]->spare_data;
  686.     ICMapEntry entry;
  687.     Handle entries=(Handle)WindowInfo[wt].items[ditList]->data;
  688.     short selection;
  689.     OSErr err=(OSErr)1;
  690.     long pos;
  691.     
  692.     GlobalToLocal(&(er->where));
  693.     selection=SelectedLine(lh);
  694.     
  695.     switch (item){
  696.         case ditAdd:
  697.             err=DoAddChange(-1,&entry,entries,lh);
  698.             break;
  699.         case ditChange:
  700.             err=DoAddChange(selection,&entry,entries,lh);
  701.             break;
  702.         case ditDelete:
  703.             if (selection!=-1){
  704.                 err=ICMapErr(ICGetIndMapEntry(GetInstance(),entries,selection+1,&pos,&entry));
  705.                 if (err==noErr)
  706.                     err=ICMapErr(ICDeleteMapEntry(GetInstance(),entries,pos));
  707.                 if (err==noErr)
  708.                     LDelRow(1,selection,lh);
  709.             }
  710.             break;
  711.         case ditList:
  712.             if (LClick(er->where,0,lh)){
  713.                 if (IsLocked(wt,item)){
  714.                     LockedAlert(wt,item);
  715.                     err=userCanceledErr;
  716.                 } else {
  717.                     FlashItem(WindowInfo[wt].window,ditChange);
  718.                     err=DoAddChange(SelectedLine(lh),&entry,entries,lh); // selection may have been changed by LClick
  719.                 }
  720.             }
  721.             break;
  722.         case ditSortExtension:
  723.         case ditSortApplication:
  724.         case ditSortName:
  725.             DoSort(wt,item);
  726.             break;
  727.         case ditDefaults:
  728.             DoSetDefaults(wt);
  729.             break;
  730.     }
  731.     
  732.     if (err==noErr)
  733.         DirtyDocument();
  734.     
  735.     if (err==(OSErr)1)
  736.         err=noErr;
  737.     
  738.     DimButtons(wt);
  739.     
  740.     return err;
  741. }
  742.  
  743. OSErr WhatFlushFileMap(WindowType wt,short item){
  744.     OSErr err=noErr;
  745.     
  746.     if (!IsLocked(wt,item))
  747.         err=ICMapErr(ICSetPrefHandle(GetInstance(),WindowInfo[wt].items[item]->key,ICattr_no_change,(Handle)WindowInfo[wt].items[item]->data));
  748.     
  749.     return err;
  750. }
  751.  
  752. OSErr WhatCloseFileMap(WindowType wt,short item){
  753.     LDispose((ListHandle)WindowInfo[wt].items[item]->spare_data);
  754.     DisposeHandle((Handle)WindowInfo[wt].items[item]->data);
  755.     return noErr;
  756. }
  757.  
  758. OSErr WhatActivateFileMap(WindowType wt,short item,Boolean activate){
  759.     LActivate(activate,(ListHandle)WindowInfo[wt].items[item]->spare_data);
  760.     return noErr;
  761. }
  762.  
  763. pascal StringPtr GetFileMapEntryName(ListHandle list,Cell c,StringPtr str){
  764.     ICMapEntry entry;
  765.     OSErr err;
  766.     long junk;
  767.     Handle entries;
  768.     
  769.     str[0]=0;
  770.     entries=(Handle)WindowInfo[GetWindowType((*list)->port)].items[ditList]->data;
  771.     err=ICMapErr(ICGetIndMapEntry(GetInstance(),entries,c.v+1,&junk,&entry));
  772.     if (err==noErr)
  773.         SetPString(str,1,entry.extension);
  774.     return str;
  775. }
  776.  
  777. OSErr WhatKeyFileMap(WindowType wt,short item,EventRecord* er){
  778.     char ch;
  779.     ICMapEntry entry;
  780.     ListHandle lh=(ListHandle)WindowInfo[wt].items[ditList]->spare_data;
  781.     OSErr err=noErr;
  782.     
  783.     if ((er->what==keyDown)||(er->what==autoKey)){
  784.         ch=er->message&0x00ff;
  785.         if ((ch==crChar)||(ch==enterChar)){
  786.             if (GetDCtlEnable(WindowInfo[wt].window,ditChange)){
  787.                 FlashItem(WindowInfo[wt].window,ditChange);
  788.                 err=DoAddChange(SelectedLine(lh),&entry,(Handle)WindowInfo[wt].items[ditList]->data,lh);
  789.                 if (err==noErr)
  790.                     DirtyDocument();
  791.             }
  792.         } else {
  793.             DoListKey(lh,er,gGetFileMapEntryName);
  794.             DimButtons(wt);
  795.         }
  796.     }
  797.     return err;
  798. }
  799.  
  800. OSErr WhatCursorFileMap(WindowType wt,short item,Point pt,short cursid){
  801.     Rect r;
  802.     CursHandle curs=GetCursor(cursid);
  803.     
  804.     GetDItemRect(WindowInfo[wt].window,item,&r);
  805.     InsetRect(&r,15,0);
  806.     if (PtInRect(pt,&r))
  807.         SetCursor(*curs);
  808.     else
  809.         InitCursor();
  810.     
  811.     return noErr;
  812. }
  813.  
  814. void MappingSetSelection(const StringPtr key){
  815.     OSErr err;
  816.     WindowPtr window=GetWindowPtr(WT_FileMapping);
  817.     long count,i,pos;
  818.     Handle entries;
  819.     ICMapEntry entry;
  820.     ListHandle list;
  821.     ICInstance inst=GetInstance();
  822.     
  823.     if (window!=(WindowPtr)0){
  824.         entries=(Handle)WindowInfo[WT_FileMapping].items[ditList]->data;
  825.         list=(ListHandle)WindowInfo[WT_FileMapping].items[ditList]->spare_data;
  826.         err=ICMapErr(ICCountMapEntries(inst,entries,&count));
  827.         if (err==noErr){
  828.             pos=0L;
  829.             for (i=1;i<=count;i++){
  830.                 err=ICMapErr(ICGetMapEntry(inst,entries,pos,&entry));
  831.                 if (err!=noErr)
  832.                     break;
  833.                 pos+=entry.total_length;
  834.                 if (IUEqualString(entry.extension,key)==0){
  835.                     LSetSingleSelection(list,i-1);
  836.                     DimButtons(WT_FileMapping);
  837.                     if (GetDCtlEnable(window,ditChange))
  838.                         err=DoAddChange(i-1,&entry,entries,list);
  839.                 }
  840.             }
  841.         }
  842.     }
  843. }
  844.  
  845.  
  846.  
  847.  
  848.